package com.hjt.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hjt.gulimall.product.dao.BrandDao;
import com.hjt.gulimall.product.dao.CategoryDao;
import com.hjt.gulimall.product.entity.BrandEntity;
import com.hjt.gulimall.product.entity.CategoryEntity;
import com.hjt.gulimall.product.service.BrandService;
import com.hjt.gulimall.product.vo.BrandResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjt.common.utils.PageUtils;
import com.hjt.common.utils.Query;

import com.hjt.gulimall.product.dao.CategoryBrandRelationDao;
import com.hjt.gulimall.product.entity.CategoryBrandRelationEntity;
import com.hjt.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandDao brandDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();

        // 查询品牌名字和分类名字，
        BrandEntity brandEntity = brandService.getById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        if (brandEntity != null) {
            categoryBrandRelation.setBrandName(brandEntity.getName());
        }
        if (categoryEntity != null) {
            categoryBrandRelation.setCatelogName(categoryEntity.getName());
        }
        baseMapper.insert(categoryBrandRelation);
    }

    // 更新品牌信息,在品牌名字更新时，要更新关联表的信息
    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setBrandName(name);
        entity.setBrandId(brandId);

        baseMapper.update(entity,new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
    }

    // 更新分类信息,在分类名字更新时，要更新关联表的信息
    @Override
    public void updateCategory(Long catId, String name) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setCatelogId(catId);
        entity.setCatelogName(name);
        baseMapper.update(entity, new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
    }

    /**
     * 根据分类id获取与该分类关联的所有品牌信息
     * @return
     */
    @Override
    public List<BrandEntity> getBrandListByCatId(Long catId) {
        List<CategoryBrandRelationEntity> relationEntityList = this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
        List<BrandEntity> brandResponseVos = relationEntityList.stream().map(relation -> {
            return brandService.getById(relation.getBrandId()); // 通过id查询
        }).collect(Collectors.toList());
        return brandResponseVos;
    }
}